﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

namespace NJL.WebUsers
{
    public partial class LocalColorInformation : BasePageVisitor
    {
        private readonly BLL.NJL_LOCAL_COLOR bll = new BLL.NJL_LOCAL_COLOR();
        protected Model.NJL_LOCAL_COLOR MODEL;
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.Model.NJL_USER modelUser = new Model.NJL_USER();
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.Model.NJL_PERSON modelPerson;
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        NJL.BLL.NJL_ATTRACTION_COMMENT bllAttrComment = new BLL.NJL_ATTRACTION_COMMENT();
        NJL.Model.NJL_ATTRACTION_COMMENT modelAttrComment = new Model.NJL_ATTRACTION_COMMENT();
        private readonly BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
        protected IList<Model.NJL_CS_TAG> LIST_TAG;
        protected IList<Model.NJL_SYS_NEWS> LIST_SYS_NEWS;

        protected void Page_Load(object sender, EventArgs e)
        {
            #region 标签
            if (WEBSITE_FOCUS_CITY_CODE != "")
            {
                LIST_TAG = bllTag.GetModelList(15, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and TAG_TYPE='2' and AREA_CODE='" + WEBSITE_FOCUS_CITY_CODE + "'", "SEARCH_COUNT desc,USED_COUNT DESC");
            }
            else
            {
                LIST_TAG = bllTag.GetModelList(15, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and TAG_TYPE='2'", "SEARCH_COUNT desc,USED_COUNT DESC");
            }
            if (LIST_TAG.Count == 0)
            {
                tag.Visible = false;
            }
            #endregion
            LoadData();
            if (!IsPostBack)
            {
                bind();
                LoadPageSEO();
            }
        }

        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            Page.Title = MODEL.SEO_TITLE;

            HtmlMeta myMeta = new HtmlMeta();
            myMeta.Name = "Keywords";
            myMeta.Content = MODEL.SEO_KEYWORDS;
            this.Header.Controls.Add(myMeta);

            HtmlMeta myMeta1 = new HtmlMeta();
            myMeta1.Name = "Description";
            myMeta1.Content = MODEL.SEO_DESCRIPTION;
            this.Header.Controls.Add(myMeta1);
        }

        private void LoadData()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                MODEL = bll.GetModel(Request.QueryString["id"]);
                if (MODEL == null)
                {
                    Response.Redirect("/Default.aspx", true);
                }
                LIST_SYS_NEWS = new BLL.NJL_SYS_NEWS().GetModelList(8, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND TYPE_CODE>20 AND IS_PUBLISH=1 ", "PUBLISH_DATE_TIME DESC");
            }
            else
            {
                Response.Redirect("/Default.aspx", true);
            }
        }



        void bind()
        {
            DataSet ds = bllAttrComment.GetList(10, "COMMENT_TYPE=2 and NJL_ATTRACTION_ID=" + Request.QueryString["id"], "CREATE_DATE_TIME desc");
            rptCommentList.DataSource = ds;
            rptCommentList.DataBind();
            litCommentCount.Text = ds.Tables[0].Rows.Count.ToString();
         

        }
        protected void btnComment_Click(object sender, EventArgs e)
        {
            if (UserId == "" || UserType == "-1")
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "请登录后再发表", "/Login.aspx?url=" + Request.RawUrl.ToString());
                return;
            }
            else
            {
                if (Session["CheckCode"] != null)
                {
                    if (txtValidate.Text.Trim().ToLower() != Session["CheckCode"].ToString().ToLower())
                    {
                        Royalsoft.Common.MessageBox.Show(this, "请输入正确的验证码");
                        return;
                    }
                    modelAttrComment.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                    modelAttrComment.COMMENT_CONTENT = txtComment.Text.Trim();
                    modelAttrComment.CREATE_USER_ID = new BasePage().UserId;
                    modelAttrComment.CREATE_USER_NAME = new BasePage().UserName;
                    modelAttrComment.CREATE_DATE_TIME = DateTime.Now;
                    modelAttrComment.NJL_ATTRACTION_ID = Request.QueryString["id"].ToString();
                    modelAttrComment.COMMENT_TYPE = 2; //地方特色为2
                    modelAttrComment.COMMENT_SCORE = int.Parse(hidScore.Value);
                    if (bllAttrComment.Add(modelAttrComment))
                    {
                        string message;
                        if (HandleIntegral(UserId, "3", out message))
                        {

                        }
                        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "评论成功+2分", "" + Request.RawUrl.ToString());
                    }
                }
                else
                {
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "连接系统超时", "" + Request.RawUrl.ToString());
                }
            }
        }
        protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView grv = (DataRowView)e.Item.DataItem;
                string userId = grv["CREATE_USER_ID"].ToString();
                Image imgUserHead = e.Item.FindControl("imgUserHead") as Image;
                modelPerson = bllPerson.GetModelByUserId(userId);
                if (modelPerson != null)
                {
                    if (modelPerson.IMAGE_CODE != "")
                    {
                        imgUserHead.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE, "U2");
                        imgUserHead.BorderWidth = 1;
                    }
                    else
                    {
                        imgUserHead.ImageUrl = "/Images/avatar.jpg";
                        imgUserHead.BorderWidth = 1;
                    }
                }
                Literal litStartSum = e.Item.FindControl("litStartSum") as Literal;
                if (!(grv["COMMENT_SCORE"] is System.DBNull))
                {
                    int COMMENT_SCORE = Convert.ToInt32(grv["COMMENT_SCORE"]);
                    for (int i = 0; i < COMMENT_SCORE; i++)
                    {
                        litStartSum.Text += "<img src='/Images/start.jpg' />";
                    }
                }
            }
        }
    }
}